<?
/**
 * ============================================================================
 * * 版权所有 2013-2017 xtoyun.net，并保留所有权利。
 * 网站地址: http://www.xtoyun.net；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: xtoyun $ 
*/
namespace xto\template;

use think\Request;

abstract class Template extends TemplateController implements ITemplate{
	private $container;
	private $_vars = [];

    /**
     * 初始当前模块
     * @access public
     * @return string
     */
    static function current(){
        $url='\\app\\'.Request::instance()->module().'\\Template';
        return new $url();
    }

    static function instance(){ 
        if (is_null ( self::$instance ) || isset ( self::$instance )) {
            self::$instance = new static ();  
        }
        return self::$instance;
    }

    /**
     * 获取Template的layout，必须要定义
     * @access public
     * @return string
     */
    abstract function getTemplateLayout();

    /**
     * 输出标签的输入函数
     * @access public
     * @return string
     */
    abstract function render();

    /**
     * 获取主健 
     * @access public
     * @return string
     */
    public function getPid(){
        return $this->getData('pid'); 
    }

    /**
     * 设置主键
     * @access public
     * @return string
     */
    public function setPid($value){
        $this->setData('pid',$value);
        return $this;
    }

    /**
     * 获取View的模板，可以外面重写来定义路径，默认是从container里面获取
     * @access public
     * @return string
     */
    

    /**
     * 获取数据源
     * @access public
     * @return string
     */
    public function getDataSource(){
        return $this->getData('datasource');
    }

    /**
     * 设置数据源
     * @access public
     * @return string
     */
    public function setDataSource($value){
        $this->setData('datasource',$value);
        return $this;
    } 

    /**
     * 获取标题
     * @access public
     * @return string
     */
    public function getTitle(){
        return $this->getData('title');
    }

    /**
     * 设置标题
     * @access public
     * @return string
     */
    public function setTitle($value){
        $this->setData('title',$value);
        return $this;
    } 

    /**
     * 获取分页
     * @access public 
     * @return string
     */
    public function getPager(){
        return $this->getData('pager');
    }

    /**
     * 设置分页
     * @access public
     * @return string
     */
    public function setPager($value){
        $this->setData('pager',$value);
        return $this;
    } 

    /**
     * 获取数据项
     * @access public
     * @return string
     */
    public function getData($key){
        if(isset($this->_vars[$key])){
            return $this->_vars[$key];
        }
    }

    /**
     * 设置数据项
     * @access public
     * @return string
     */
    public function setData($key,$value){
        if(!isset($this->_vars[$key])){
            $this->_vars[$key]=$value;
        }
        return $this;
    } 

    /**
     * 设置数据对象
     * @access public
     * @return string
     */
    public function setDataList($key,$value){
        $this->_vars[$key][]=$value;
        return $this;
    }

    public function getViewLayout(){
        //return APP_PATH."common/view/$this->theme/admin/layout.html";
        return $this->container->ViewLayout;
    }

    public function getModule(){ 
        return $this->container->module;
    }

    public function getAction(){
        return $this->container->action;
    }

    public function getController(){
        return $this->container->controller;
    }

    public function __construct(TemplateContainer $tc){
        parent::__construct(); 
        $this->container=$tc;  
        $this->setData('module',$this->module);
        $this->setData('action',$this->action);
        $this->setData('controller',$this->controller);
    }

	//public function _initialize(){  
        //parent::_initialize();  
        
        //$this->setData('layout',$this->ViewLayout);
	//}

	public function fetch($template = '', $vars = [], $replace = [], $config = [])
    {
        $this->render();
    	if (empty($template)) {
    		$template=$this->getTemplateLayout();
    	}
        $vars = array_merge($vars, $this->_vars);
        //$this->assign('layout',$this->layout); 
        //$this->assign('template',$this->TemplateLayout);
        //dump($this->layout);
        //dump($template);
        //die;
        return parent::fetch($template, $vars, $replace, $config); 
    } 

    public function view($template = '', $vars = [], $replace = []){
 
        $this->render();
        if (empty($template)) {
            $template=$this->getTemplateLayout();
        }
        //$this->assign('layout',$this->layout); 
        //$this->assign('template',$this->TemplateLayout);
        $vars = array_merge($vars, $this->_vars);
        return \think\Response::create($template, 'view', 200)->replace($replace)->assign($vars);
    }

    public function __get($name)              // 这里$name是属性名
    {
        $getter = 'get' . $name;              // getter函数的函数名
        if (method_exists($this, $getter)) {
            return $this->$getter();          // 调用了getter函数
        } else {
            if (isset($this->$name)) {
                return $this->$name;
            }
        }
    }

    public function __set($name, $value)
    {
        $setter = 'set' . $name;             // setter函数的函数名
        if (method_exists($this, $setter)) {
            $this->$setter($value);          // 调用setter函数
        } else {
            $this->$name = $value; 
        }
    }
}